package oa.hleast.xswl.service.place.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.utils.ImagerListUtil;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.mapper.place.PlaceInfoCommentMapper;
import oa.hleast.xswl.pojo.domain.place.CmsPlaceInfoComment;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceInfoCommentVo;
import oa.hleast.xswl.service.place.IPlaceInfoCommentService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 302
 * @description
 */

@Transactional
@Slf4j
@Service
@AllArgsConstructor
public class PlaceInfoCommentServiceImpl extends ServiceImpl<PlaceInfoCommentMapper, CmsPlaceInfoComment> implements IPlaceInfoCommentService {

    private final PlaceInfoCommentMapper placeInfoCommentMapper;

    @Override
    public Integer selectCount(Long commentId, Long userId) {

        QueryWrapper<CmsPlaceInfoComment> condition = new QueryWrapper<CmsPlaceInfoComment>()
                .eq("id", commentId)
                .eq("comment_user_id", userId)
                .last("limit 1");
        return placeInfoCommentMapper.selectCount(condition);
    }

    /**
     * ================== 基础功能 ===================
     */

    @Override
    public boolean deleteByIds(List<String> asList) {
        if (CollectionUtils.isFull(asList)) {
            throw new BizException("没有选择任何数据，请重新操作！");
        }
        return this.removeByIds(asList);
    }

    @Override
    public Page<CmsPlaceInfoCommentVo> pageList(Integer page, Integer limit, String placeTypeId) {
        LambdaQueryWrapper<CmsPlaceInfoComment> queryWrapper = new LambdaQueryWrapper<CmsPlaceInfoComment>()
                .eq(CmsPlaceInfoComment::getPlaceInfoId, placeTypeId)
                .orderByDesc(CmsPlaceInfoComment::getGmtModified)
                .orderByDesc(CmsPlaceInfoComment::getGmtCreate);

        Page<CmsPlaceInfoComment> complaintSuggestPage = this.page(new Page<>(page, limit), queryWrapper);

        if (complaintSuggestPage.getTotal() == 0) {
            return new Page<>(page, limit);
        }

        List<CmsPlaceInfoCommentVo> complaintSuggestList = complaintSuggestPage.getRecords().stream().map(this::conversionToVo).collect(Collectors.toList());

        Page<CmsPlaceInfoCommentVo> voPage = new Page<>(page, limit, complaintSuggestPage.getTotal());
        voPage.setRecords(complaintSuggestList);

        return voPage;
    }

    /**
     * =================  私有方法 ==================
     */

    private CmsPlaceInfoCommentVo conversionToVo(CmsPlaceInfoComment pojo) {

        CmsPlaceInfoCommentVo vo = new CmsPlaceInfoCommentVo();
        BeanUtil.copyProperties(pojo, vo);

        // 添加格式化时间
        vo.setGmtCreateDate(pojo.getGmtCreate());
        // 转换图片列表
        vo.setImageList(ImagerListUtil.conversionToList(pojo.getImageList()));

        return vo;
    }
}
